package com.shuwen.gcdj.common.util;

import com.shuwen.gcdj.bean.dept.response.DeptDetailResp;
import com.shuwen.gcdj.bean.equipmentMaintenance.response.WorkOrderStatisticsResp;
import com.shuwen.gcdj.bean.structure.response.StrategyResp;
import com.shuwen.gcdj.service.statistic.WorkingConditionsService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author XYC
 * 嗨
 */
@Slf4j
public class FieldUtil {

    //用于对象字段过多数据都在字段中时可以根据穿的fileName 获取匹配字段名称的字段value值；
    public static <U> String usersBlankNum(String fielName, Class<U> clazz, U cla) {
        U u = clazz.cast(cla);
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            if (field.getName().equals(fielName)) {
                try {
                    return field.get(u) == null ? null : field.get(u).toString();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    //list获取指定字段 用于特殊的表数据；
    public static <U> List<String> usersBlankNum(String fielName, Class<U> clazz, List<U> cla) {
        List<String> result = new ArrayList<>();
        for (U cl : cla) {

            Field field = null;
            try {
                field = clazz != null ? clazz.getDeclaredField(fielName) : null;
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            field.setAccessible(true);
            String value = null;
            if (field.getName().equals(fielName)) {
                try {
                    value = field.get(cl) == null ? null : field.get(cl).toString();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            result.add(value);
        }
        return result;
    }


    //list获取指定字段 用于特殊的表数据；返回plc 状态表数据 bool
    public static <U> List<StrategyResp> usersBlankBoolean(List<String> fielName, Class<U> clazz, U cla) {
        List<StrategyResp> values = new ArrayList<>();
        U u = clazz.cast(cla);
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            StrategyResp resp = new StrategyResp();
            if (fielName.contains(field.getName())) {
                try {
                    resp.setSensorCode(field.getName());
                    resp.setFLag(field.get(u) == null ? null : (Boolean) field.get(u));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            values.add(resp);
        }
        return values;
    }


    //测试
    public static void main(String[] args) {
        String a = "({0}+2)*3+{0}";
        a = a.replace("{0}", "1");
        double b = WorkingConditionsService.eval(a);

//        List<DeptDetailResp> a = new ArrayList<>();
//        DeptDetailResp b = new DeptDetailResp();
//        b.setName("公司");
//        DeptDetailResp c = new DeptDetailResp();
//        c.setName("公司1");
//        a.add(b);
//        a.add(c);
//        String name = "name";
//        List<String> d = usersBlankNum(name, DeptDetailResp.class, a);
    }
}
